package com.swgk.core.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.swgk.core.R;

/**
 * @ProjectName :物流详情页复用item
 * @Author : XiongLi
 * @Time :
 */
public class DetailsItemTextView extends RelativeLayout implements View.OnClickListener {
    private Context context;
    private TextView info,tv_send,tv_send_name,tv_phone,tv_phone_name,send_call_phone,info_content,send_call_phonetext;
    private boolean isVisiabel,infoVisiabel,isVisiabelText;
    private CallPhoneClickListener mclickListener;
    private RelativeLayout rl_contack_one,rl_contack_two,rl_contact_three;
    public DetailsItemTextView(Context context) {
        this(context, null);
    }

    public DetailsItemTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DetailsItemTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context= context;
        init();
        setAttrs(attrs);
    }
    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_details_text, this);
            info=view.findViewById(R.id.info);
            tv_send=view.findViewById(R.id.title1);
            tv_send_name=view.findViewById(R.id.title1_content);
            tv_phone=view.findViewById(R.id.title2);
            tv_phone_name=view.findViewById(R.id.title2_content);
            send_call_phone=view.findViewById(R.id.send_call_phone);
            send_call_phonetext = view.findViewById(R.id.send_call_phone_text);
            send_call_phone.setVisibility(VISIBLE);
            info_content = view.findViewById(R.id.info_content);
            rl_contack_one = view.findViewById(R.id.rl_contack_one);
            rl_contack_two = view.findViewById(R.id.rl_contack_two);
            rl_contact_three = view.findViewById(R.id.rl_contact_three);
    }
    private void setAttrs(AttributeSet attrs){
        if (attrs!=null){
            TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.DetailsItemTextView);
            info.setText(typedArray.getString(R.styleable.DetailsItemTextView_info_name));
            tv_send.setText(typedArray.getString(R.styleable.DetailsItemTextView_title1));
            tv_send_name.setText(typedArray.getString(R.styleable.DetailsItemTextView_title1_content));
            tv_phone.setText(typedArray.getString(R.styleable.DetailsItemTextView_title2));
            tv_phone_name.setText(typedArray.getString(R.styleable.DetailsItemTextView_title2_content));
            send_call_phone.setText(typedArray.getString(R.styleable.DetailsItemTextView_tv_call_phone));
            isVisiabel = typedArray.getBoolean(R.styleable.DetailsItemTextView_call_phone_isvisit,true);
            info_content.setText(typedArray.getString(R.styleable.DetailsItemTextView_info_content));
            infoVisiabel = typedArray.getBoolean(R.styleable.DetailsItemTextView_info_content_isvisit,false);
            //隐藏电话
            if (isVisiabel){
                send_call_phone.setVisibility(VISIBLE);
            }else {
                send_call_phone.setVisibility(GONE);
            }
            if (isVisiabelText){
                send_call_phonetext.setVisibility(VISIBLE);
            }else {
                send_call_phonetext.setVisibility(GONE);
            }
            //设置info内容是否显示
            if (infoVisiabel){
                info.setTextColor(getResources().getColor(R.color.color_tab_two));
                info.setTextSize(14);
                info_content.setVisibility(VISIBLE);
            }
            send_call_phone.setOnClickListener(this);
            send_call_phonetext.setOnClickListener(this);
            Drawable img = getResources().getDrawable(typedArray.getResourceId(R.styleable.DetailsItemTextView_call_left_image,R.drawable.detail_phone));
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
            send_call_phone.setCompoundDrawables(img, null, null, null); //设置左图标
            send_call_phonetext.setCompoundDrawables(img, null, null, null); //设置左图标
        }
    }

    @Override
    public void onClick(View v) {
        if (mclickListener==null)return;
            v.setId(getId());
            mclickListener.callPhone(v);
    }

    public interface CallPhoneClickListener{
        void callPhone(View view);
    }

    /**
     * 设置第二条内容
     * @param content
     */
    public void setContent2(String content) {
        if (!TextUtils.isEmpty(content)) {
            tv_phone_name.setText(content);
        } else {
            tv_phone_name.setText("");
        }
    }

    /**
     * 设置第一天内容
     * @param content
     */
    public void setContent1(String content) {
        if (!TextUtils.isEmpty(content)) {
            tv_send_name.setText(content);
        } else {
            tv_send_name.setText("");
        }
    }
    /**
     * 设置标题1
     */
    public void setTitleOne(String titleOne){
        if (!TextUtils.isEmpty(titleOne)){
            tv_send.setText(titleOne);
        }else {
            tv_send.setText("");
        }
    }
    /**
     * 设置标题2
     */
    public void setTitleTwo(String titleTwo){
        if (!TextUtils.isEmpty(titleTwo)){
            tv_phone.setText(titleTwo);
        }else {
            tv_phone.setText("");
        }
    }
    public void setClickListener(CallPhoneClickListener clickListener){
        this.mclickListener = clickListener;
    }
    /**
     * 设置发货人内容
     */
    public void setInfoContent(String infoContent){
        if (!TextUtils.isEmpty(infoContent)){
            info_content.setText(infoContent);
        }else {
            info_content.setText("");
        }
    }
    /**
     * 设置拨打电话隐藏
     */
    public void setCallPhoneVisibility(boolean isVisiabel){
        send_call_phone.setVisibility(isVisiabel?VISIBLE:GONE);
    }
    /**
     * 设置拨打电话隐藏
     */
    public void setCallPhoneVisibilityText(boolean isVisiabel){
        send_call_phonetext.setVisibility(isVisiabel?VISIBLE:GONE);
    }
    /**
     * 隐藏整行
     */
    public void setVisiabelRlOne(boolean one,boolean two,boolean three){
        rl_contack_one.setVisibility(one?GONE:VISIBLE);
        rl_contack_two.setVisibility(two?GONE:VISIBLE);
        rl_contact_three.setVisibility(three?GONE:VISIBLE);
    }
    /**
     * 设置颜色
     */
    public void setContextColor(int color){
        tv_send_name.setTextColor(color);
    }
}
